package com.xci.sys.controller;

import com.xci.core.annotation.Authorize;
import com.xci.core.base.RestMessage;
import com.xci.core.web.JsonGrid;
import com.xci.core.exceptions.EntityNotFoundException;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.helper.Helper;
import com.xci.core.base.WebController;
import com.xci.sys.entity.SysParam;
import com.xci.sys.filter.ParamFilter;
import com.xci.sys.service.ParamService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 参数控制器
 */
@Authorize
@Controller("sysParamController")
@RequestMapping("/sys/param")
public class ParamController extends WebController {
    @Resource
    private ParamService paramService;//参数服务

    //region 页面视图

    /**
     * 系统参数首页
     */
    @GetMapping
    @Authorize(code = "sys.param")
    public String index() {
        return "sys/param/index";
    }

    /**
     * 系统参数新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "sys.param.insert")
    public String create(ModelMap map) {
        createMark(map);
        var entity = new SysParam();
        entity.setId(Helper.nextIdStr());
        map.put("entity", entity);
        return "sys/param/edit";
    }

    /**
     * 参数编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "sys.param.update")
    public String edit(String id, ModelMap map) {
        var entity = paramService.selectById(id);
        if (entity == null) throw new EntityNotFoundException(id);
        map.put("entity", entity);
        return "sys/param/edit";
    }

    /**
     * 系统参数详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        var entity = paramService.selectById(id);
        if (entity == null) throw new EntityNotFoundException(id);
        map.put("entity", entity);
        return "sys/param/details";
    }

    /**
     * 参数设置页
     */
    @GetMapping("/setting")
    public String setting(ModelMap map) {
        List<SysParam> list = paramService.selectList(new ParamFilter());
        for (SysParam entity : list) {
            map.put(entity.getCode(), entity.getValue());
        }
        return "sys/param/setting";
    }

    /**
     * 参数设置保存
     */
    @ResponseBody
    @PostMapping("/setting-save")
    public RestMessage settingSave(@RequestParam Map<String,String> params) {
       List<SysParam> list = paramService.selectList(new ParamFilter());
       for (var entity : list) {
           if (!params.containsKey(entity.getCode())){
               continue;
           }
           String val = params.get(entity.getCode());
           if (val.equals(entity.getValue())){
               continue;
           }
           entity.setValue(val);
           paramService.update(entity);
       }
        return RestMessage.success();
    }

    //endregion

    //region 数据处理

    /**
     * 系统参数表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "sys.param.select")
    public JsonGrid grid(ParamFilter filter) {
        return RestMessage.grid(paramService.selectList(filter));
    }

    /**
     * 保存新增系统参数
     */
    @ResponseBody
    @PostMapping("/createSave")
    @Authorize(code = "sys.param.insert")
    public RestMessage createSave(@ModelAttribute SysParam entity) {
        return paramService.insert(entity);
    }

    /**
     * 保存修改系统参数
     */
    @ResponseBody
    @PostMapping("/updateSave")
    @Authorize(code = "sys.param.update")
    public RestMessage updateSave(@ModelAttribute SysParam entity) {
        return paramService.update(entity);
    }

    /**
     * 删除系统参数
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "sys.param.delete")
    public RestMessage delete(String ids) {
        return paramService.delete(ids);
    }

    /**
     * 导出系统参数
     */
    @GetMapping("/export")
    @Authorize(code = "sys.param.export")
    public void export(ParamFilter filter) {
        ExcelHelper.exportWeb(paramService.selectList(filter), SysParam.class, "系统参数列表");
    }

    //endregion
}